package com.hudongwx.sadpw.fragment.foodmenu;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.activity.FoodDetailActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_hao_you)
public class HaoYouFragment extends Fragment {
    private View view;
//    @ViewInject(R.id.haoyou_rec_btn)
//    private Button button;
    private String TAG="HaoYouFragment";
//    @ViewInject(R.id.haoyou_list)
//    private ListView listView;
    @ViewInject(R.id.haoyou_rec)
    private RecyclerView recyclerView;
    MyHaoYouAdapter myHaoYouAdapter;
    JSONArray jsonArray;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       if (null==view){
           view=x.view().inject(this,inflater,container);
           LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
           recyclerView.setLayoutManager(linearLayoutManager);
           myHaoYouAdapter=new MyHaoYouAdapter(jsonArray);
           recyclerView.setAdapter(myHaoYouAdapter);
           parseHaoYou();

       }
        return view;
    }
    //解析数据

    class MyAdapter extends BaseAdapter{
        JSONArray jsonArray;

        public MyAdapter(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }
        public void updateList(JSONArray jsonArray){
            this.jsonArray=jsonArray;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
        class MyListViewHoolder{
            ImageView imageView;
            TextView textView;
        }
    }
    //解析json
    public void parseHaoYou(){
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Product?brand=CW");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myHaoYouAdapter.updateRec(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    class MyHaoYouAdapter extends RecyclerView.Adapter<MyHaoYouViewHolder>{
        JSONArray jsonArray;

        public MyHaoYouAdapter(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }
        public void updateRec(JSONArray jsonArray){
            this.jsonArray=jsonArray;
            notifyDataSetChanged();
        }
        @Override
        public MyHaoYouViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getActivity().getLayoutInflater().inflate(R.layout.haoyou_recycle, null);
            return new MyHaoYouViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyHaoYouViewHolder holder, final int position) {
            if (null==jsonArray){
                recyclerView.setVisibility(View.GONE);
            }else {
                try {
                    holder.textView1.setText(jsonArray.getJSONObject(position).getString("ProductName"));
                    Log.i(TAG, "onBindViewHolder: ProductName:"+jsonArray.toString());
                    holder.textView2.setText(jsonArray.getJSONObject(position).getString("ZMShortDescribes"));
                    x.image().bind(holder.imageView1,jsonArray.getJSONObject(position).getString("HomeImgUrl"));

                    holder.button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getContext(), FoodDetailActivity.class);
                            try {
                                String string=jsonArray.getJSONObject(position).getString("ProductID");
                                intent.putExtra("type",string);
                                getActivity().startActivity(intent);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemCount() {
            if (null==jsonArray)return 0;
            return jsonArray.length();
        }
    }
    class MyHaoYouViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView1,imageView2;
        TextView textView1,textView2;
        Button button;

        public MyHaoYouViewHolder(View itemView) {
            super(itemView);
            imageView1= (ImageView) itemView.findViewById(R.id.haoyou_rec_iv_1);
            imageView2= (ImageView) itemView.findViewById(R.id.haoyou_rec_iv_2);
            textView1= (TextView) itemView.findViewById(R.id.haoyou_rec_tv_1);
            textView2= (TextView) itemView.findViewById(R.id.haoyou_rec_tv_2);
            button= (Button) itemView.findViewById(R.id.haoyou_rec_btn);
        }
    }
}
